package dog.weatherwallpaper.model;

import org.json.JSONObject;

import dog.weatherwallpaper.R;
import dog.weatherwallpaper.utils.App;
import dog.weatherwallpaper.utils.Time;

public final class CurrentWeather extends AbstractWeather {
	private int cloudcover;
	private int humidity;
	/**
	 * Coordinated Universal Time (UTC). Format hh:mm aa
	 */
	private String observation_time = "00:00";
	private int pressure;
	private int temp_C;
	private int temp_F;
	private int visibility;

	CurrentWeather(final JSONObject json) {
		super(json);
		if (json == null) return;
		this.cloudcover = json.optInt("cloudcover");
		this.humidity = json.optInt("humidity");
		this.observation_time = json.optString("observation_time");
		this.pressure = json.optInt("pressure");
		this.temp_C = json.optInt("temp_C");
		this.temp_F = json.optInt("temp_F");
		this.visibility = json.optInt("visibility");
	}

	@Override
	public String getBasicInfo(Unit unit) {
		StringBuilder sb = new StringBuilder();
		sb.append(this.get24HourTime());
		sb.append(SPACE);
		sb.append(this.temperature(unit.isCelsius()));
		if (precipMM != 0) {
			sb.append(SPACE);
			sb.append(super.getPrecipitation());
		}
		if (visibility != 10) {
			sb.append(SPACE);
			sb.append(this.getVisibility(unit.isKm()));
		}
		sb.append(SPACE);
		sb.append(super.getWeatherDesc());
		return sb.toString();
	}

	@Override
	public String getAllInfo(Unit unit) {
		StringBuilder sb = new StringBuilder();
		sb.append(createLine(R.string.time, this.get24HourTime()));
		sb.append(createLine(R.string.temperature, this.temperature(unit.isCelsius())));
		sb.append(createLine(R.string.description, super.getWeatherDesc()));
		sb.append(createLine(R.string.humidity, this.getHumidity()));
		sb.append(createLine(R.string.pressure, this.getPressure()));
		if (windspeedKmph != 0) {
			sb.append(createLine(R.string.wind, super.getWindSpeed(unit.isKm()) + super.getWindDirection()));
		}
		if (precipMM != 0) {
			sb.append(createLine(R.string.precipitation, this.getPrecipitation()));
		}
		if (visibility != 10) {
			sb.append(createLine(R.string.visibility, this.getVisibility(unit.isKm())));
		}
		if (cloudcover != 0) {
			sb.append(createLine(R.string.cloud_cover, this.getCloudcover()));
		}
		return sb.toString();
	}

	public String getExtraInfo(final boolean km) {
		StringBuilder sb = new StringBuilder();
		sb.append(SPACE);
		sb.append(createField(R.string.humidity, this.getHumidity()));
		sb.append(createField(R.string.pressure, this.getPressure()));
		if (windspeedKmph != 0) {
			sb.append(createField(R.string.wind, super.getWindSpeed(km)));
		}
		if (cloudcover != 0) {
			sb.append(createField(R.string.cloud_cover, this.getCloudcover()));
		}
		return sb.toString();
	}

	String getTime() {
		return observation_time;
	}

	private String temperature(final boolean celsius) {
		if (celsius) {
			return temp_C + App.context().getString(R.string.c);
		} else {
			return temp_F + App.context().getString(R.string.f);
		}
	}

	private String getVisibility(final boolean km) {
		if (km) {
			return visibility + App.context().getString(R.string.km);
		} else {
			int miles = (int) (visibility * 0.6214);
			return miles + App.context().getString(R.string.mi);
		}
	}

	private String getHumidity() {
		return humidity + "%";
	}

	private String getCloudcover() {
		return cloudcover + "%";
	}

	private String getPressure() {
		return pressure + "mb";
	}

	private String get24HourTime() {
		return new Time().formatTime(observation_time);
	}
}